package com.fpt.edu.vn.h2tv.api.admin;

import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.service.UserService;

@Path("/manageStaff")
public class StaffAdminAPI {
    
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)    
    public Response manageStaff(){
        
        return buildResponse(MediaType.APPLICATION_XML, UserService.getAllStaff());
    }
    
    @Path("/getAllStaff")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)    
    public Response getStaff(){
        
        return buildResponse(MediaType.APPLICATION_XML, UserService.getStaff());
    }
    
    @Path("/getAllAdmin")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)    
    public Response getAdmin(){
        
        return buildResponse(MediaType.APPLICATION_XML, UserService.getAllAdmin());
    }
    
    @Path("/searchStaff")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)    
    public Response searchStaff(@QueryParam(value="txtUserName") String username){
        
        return buildResponse(MediaType.APPLICATION_XML, UserService.searchStaff(username));
    }
    
    @POST
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response addNewStaff(@FormParam(value="txtUserName") String username, 
            @FormParam(value="txtLastName") String lname, @FormParam(value="txtFirstName") String fname,
            @FormParam(value="txtPwd") String pwd, @FormParam(value="txtPhone") String phone, @FormParam(value="txtEmail") String email,
            @FormParam(value="txtAddress") String address, @FormParam(value="txtRole") String role){
        
        return buildResponse(MediaType.TEXT_HTML, UserService.addStaff(username, pwd, fname, lname, 
                phone, address, role, email));
    }
    
    @DELETE
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response deleteStaff(@QueryParam(value="txtUserId") Long id){
        
        return buildResponse(MediaType.TEXT_HTML, UserService.deleteAccount(id));
    }
    
    @PUT
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response updateStaff(@QueryParam(value="txtUpdateUserId") Long id, @QueryParam(value="txtUpdateLastName") String lname, 
            @QueryParam(value="txtUpdateFirstName") String fname, @QueryParam(value="txtUpdatePhone") String phone, @QueryParam(value="txtUpdateAddress") String address, 
            @QueryParam(value="txtUpdateRole") String role, @QueryParam(value="txtUpdateIsBan") Boolean ban, @QueryParam(value="txtEmail") String email){
        
        return buildResponse(MediaType.TEXT_HTML, UserService.updateStaff(id, lname, fname, phone, address, role, ban, email));
    }
    
    @OPTIONS
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response test(){
            
        return buildResponse(MediaType.TEXT_HTML, "ok");
    }
    
    private Response buildResponse(String mediaType, Object repsonse){
        return Response.ok(repsonse, mediaType)
//                .header("Access-Control-Allow-Origin", "http://localhost")
//                .header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,CONNECT")
//                .header("Access-Control-Allow-Credentials",true)
//                .header("Access-Control-Allow-Headers", "x-accept-charset, x-accept, origin,X-Accept-Charset,X-Accept,Content-Type,Authorization,REFERER")
                .build();
    }

}
